﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ecreate.Business;
using Ecreate.Model;

namespace Ecreate.Web.systemanage.news
{
    public partial class luckinfo : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string type = Request.QueryString["type"];
                if (type == "del")
                {
                    string id = Request.QueryString["id"];
                    string sql = "delete from t_luck_info where id=" + id;
                    int ret = usercontrol.GG.ExecSql(sql);
                    if (ret != -1)
                    {
                        usercontrol.GG.AlertAndRedirectChild(this, "删除成功。", "/systemanage/news/luckinfolist.aspx");
                    }
                    else
                    {
                        usercontrol.GG.Alert(this, "删除失败。");
                    }
                }
                else if (type == "edit")
                {
                    string id = Request.QueryString["id"];
                    string sql = "select * from t_luck_info where id=" + id;
                    System.Data.DataTable dt = usercontrol.GG.GetTable(sql);
                    if (dt != null && dt.Rows.Count > 0)
                    {
                        System.Data.DataRow dr = dt.Rows[0];
                        txtName.Text = dr["luck_name"].ToString();
                        txtTel.Text = dr["luck_tel"].ToString();
                        txtMoney.Text = dr["luck_money"].ToString();
                    }
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                LuckInfo news;
                string id = "";
                if (Request.QueryString["id"] != null)
                {
                    id = Request.QueryString["id"].ToString();
                }
                if (id == "")
                {
                    news = new LuckInfo();
                }
                else
                {
                    news = new LuckInfo(id);
                }
                news.LuckName = txtName.Text;
                news.LuckTel = txtTel.Text;
                decimal d;
                if (decimal.TryParse(txtMoney.Text,out d))
                {
                    news.LuckMoney = Convert.ToDecimal(txtMoney.Text);
                }
                else
                {
                    news.LuckMoney = 0;
                }
                LuckInfoService service = new LuckInfoService();
                if (id == "")
                {
                    service.Insert(news);
                }
                else
                {
                    service.Update(news);
                }
                usercontrol.GG.AlertAndRedirectChild(this, "保存成功。", "/systemanage/news/luckinfolist.aspx");
            }
            catch
            {
                //usercontrol.GG.Alert(this, "保存失败。");
            }
        }
    }
}
